<template>
  <div class="messages-page">
    <div class="page-header messages-header">
      <div class="header-content">
        <h1 class="page-title">消息</h1>
      </div>
      
      <div class="search-container">
        <Search :size="13" class="search-icon" />
        <input 
          v-model="searchQuery"
          type="text" 
          placeholder="搜索..."
          class="search-input"
        />
      </div>
    </div>

    <div 
      class="page-content messages-content"
      :style="{ opacity: contentOpacity, transform: `translateY(${contentTransform}px)` }"
    >
      <div v-if="filteredMessages.length === 0" class="empty-state">
        <MessageCircle :size="64" class="empty-icon" />
        <h3>暂无消息</h3>
        <p>开始与邻居们交流吧！</p>
      </div>
      
      <div v-else class="messages-list">
        <div 
          v-for="message in filteredMessages"
          :key="message.id"
          class="message-item"
          @click="openChat(message)"
        >
          <div class="message-avatar">
            <span class="avatar-emoji">{{ message.avatar }}</span>
            <div v-if="message.isOnline" class="online-indicator"></div>
          </div>
          
          <div class="message-content">
            <div class="message-header">
              <span class="sender-name">{{ message.sender }}</span>
              <span class="message-time">{{ message.time }}</span>
            </div>
            
            <div class="last-message">{{ message.lastMessage }}</div>
            
            <div class="message-meta">
              <span 
                v-if="message.type !== 'normal'"
                :class="['message-type', message.type]"
              >
                {{ getTypeLabel(message.type) }}
              </span>
            </div>
          </div>
          
          <div class="message-status">
            <div v-if="message.unreadCount > 0" class="unread-badge">
              {{ message.unreadCount }}
            </div>
          </div>
        </div>
      </div>
    </div>

    <button 
      class="fab"
      :style="{ opacity: fabOpacity, transform: `scale(${fabScale})` }"
      @click="startNewChat"
    >
      <Plus :size="24" />
    </button>
  </div>
</template>

<script>
import { Search, MessageCircle, Plus } from 'lucide-vue-next'

export default {
  name: 'Messages',
  components: {
    Search,
    MessageCircle,
    Plus
  },
  data() {
    return {
      searchQuery: '',
      activeFilter: 'all',
      messages: [],

      contentOpacity: 0,
      contentTransform: 20,
      fabOpacity: 0,
      fabScale: 0.8,
      filters: [
        { key: 'all', label: '全部', count: 0 },
        { key: 'unread', label: '未读', count: 0 }
      ]
    }
  },
  computed: {
    filteredMessages() {
      let filtered = this.messages
      
      // 按类型筛选
      if (this.activeFilter !== 'all') {
        if (this.activeFilter === 'unread') {
          filtered = filtered.filter(msg => msg.unreadCount > 0)
        } else {
          filtered = filtered.filter(msg => msg.type === this.activeFilter)
        }
      }
      
      // 按搜索关键词筛选
      if (this.searchQuery.trim()) {
        const query = this.searchQuery.toLowerCase()
        filtered = filtered.filter(msg => 
          msg.sender.toLowerCase().includes(query) ||
          msg.lastMessage.toLowerCase().includes(query)
        )
      }
      
      return filtered
    }
  },
  mounted() {
    this.initializeAnimations()
    this.loadMockMessages()
  },
  methods: {
    initializeAnimations() {
      setTimeout(() => {
        this.contentOpacity = 1
        this.contentTransform = 0
      }, 300)
      
      setTimeout(() => {
        this.fabOpacity = 1
        this.fabScale = 1
      }, 500)
    },
    
    loadMockMessages() {
      const mockMessages = [
        {
          id: 1,
          sender: '小明',
          avatar: '👨‍💻',
          lastMessage: '你好！我看到你会Python编程，能帮我看看这个代码吗？',
          time: '2分钟前',
          unreadCount: 2,
          isOnline: true,
          type: 'skill'
        },
        {
          id: 2,
          sender: '小红',
          avatar: '👩‍🎨',
          lastMessage: '谢谢你借给我相机！拍出来的照片很棒 📸',
          time: '10分钟前',
          unreadCount: 0,
          isOnline: true,
          type: 'device'
        },
        {
          id: 3,
          sender: '老王',
          avatar: '👨‍🔧',
          lastMessage: '我的工具箱明天可以借给你，记得小心使用哦',
          time: '1小时前',
          unreadCount: 1,
          isOnline: false,
          type: 'device'
        },
        {
          id: 4,
          sender: '小李',
          avatar: '👨‍🏫',
          lastMessage: '周末的编程学习小组还有位置，要不要一起来？',
          time: '2小时前',
          unreadCount: 0,
          isOnline: true,
          type: 'skill'
        },
        {
          id: 5,
          sender: '系统消息',
          avatar: '🤖',
          lastMessage: '恭喜！你的技能等级提升了，快去查看新解锁的功能吧！',
          time: '3小时前',
          unreadCount: 1,
          isOnline: false,
          type: 'system'
        },
        {
          id: 6,
          sender: '小张',
          avatar: '👩‍🍳',
          lastMessage: '今天做的蛋糕很成功，谢谢你的烘焙技巧指导！',
          time: '昨天',
          unreadCount: 0,
          isOnline: false,
          type: 'skill'
        },
        {
          id: 7,
          sender: '小刘',
          avatar: '👨‍🎵',
          lastMessage: '吉他借用完毕，已经放回原处，音质真的很棒！',
          time: '昨天',
          unreadCount: 0,
          isOnline: true,
          type: 'device'
        }
      ]
      
      this.messages = mockMessages
      this.updateFilterCounts()
    },
    
    updateFilterCounts() {
      this.filters.forEach(filter => {
        if (filter.key === 'all') {
          filter.count = this.messages.length
        } else if (filter.key === 'unread') {
          filter.count = this.messages.filter(msg => msg.unreadCount > 0).length
        } else {
          filter.count = this.messages.filter(msg => msg.type === filter.key).length
        }
      })
    },
    
    setActiveFilter(filterKey) {
      this.activeFilter = filterKey
    },
    
    getTypeLabel(type) {
      const typeLabels = {
        skill: '技能',
        device: '设备',
        system: '系统'
      }
      return typeLabels[type] || ''
    },
    
    openChat(message) {
      console.log('打开聊天:', message.sender)
      // 这里可以导航到具体的聊天页面
    },
    
    startNewChat() {
      console.log('发起新对话')
      // 这里可以打开新建对话的界面
    }
  }
}
</script>

<style scoped>
.messages-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f4e8ed;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  overflow: hidden;
}

.messages-header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  z-index: 100;
}

.search-container {
  position: relative;
  margin: 20px 4px 0 4px;
  max-width: none;
}

.search-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  z-index: 1;
}

.search-input {
  width: 100%;
  padding: 12px 16px 12px 44px;
  border: none;
  border-radius: 10px;
  background: #f5f5f5;
  font-size: 16px;
  outline: none;
  transition: all 0.3s ease;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: #333;
  box-shadow: none;
}

.search-input:focus {
  background: #f0f0f0;
  box-shadow: none;
}

.search-input::placeholder {
  color: #999;
  opacity: 1;
}

.messages-content {
  top: 140px;
  padding: 0 20px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.9);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.empty-icon {
  opacity: 0.6;
  margin-bottom: 24px;
}

.empty-state h3 {
  margin: 0 0 12px 0;
  font-size: 24px;
  font-weight: 600;
  color: white;
}

.empty-state p {
  margin: 0;
  opacity: 0.8;
  font-size: 16px;
  color: rgba(255, 255, 255, 0.8);
}

.messages-list {
  padding: 20px 0;
}

.message-item {
  display: flex;
  align-items: center;
  padding: 16px;
  margin-bottom: 12px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  border: none;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  backdrop-filter: blur(20px);
}

.message-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  background: white;
}

.message-avatar {
  position: relative;
  margin-right: 16px;
}

.avatar-emoji {
  font-size: 44px;
  display: block;
  filter: none;
}

.online-indicator {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  background: #48bb78;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.message-content {
  flex: 1;
  min-width: 0;
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.sender-name {
  font-weight: 600;
  color: #2d3748;
  font-size: 16px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.message-time {
  font-size: 12px;
  color: #a0aec0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.last-message {
  color: #718096;
  font-size: 14px;
  line-height: 1.4;
  margin-bottom: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.message-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

.message-type {
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  border: none;
}

.message-type.skill {
  background: rgba(72, 187, 120, 0.1);
  color: #48bb78;
}

.message-type.device {
  background: rgba(237, 100, 166, 0.1);
  color: #ed64a6;
}

.message-type.system {
  background: rgba(236, 201, 75, 0.1);
  color: #ecc94b;
}

.message-status {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.unread-badge {
  background: #667eea;
  color: white;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  animation: pulse 2s infinite;
  border: none;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.fab {
  position: fixed;
  bottom: 100px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 1000;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.fab:hover {
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 12px 32px rgba(102, 126, 234, 0.4);
}

.fab:active {
  transform: translateY(0) scale(0.95);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
}



/* 响应式设计 */
@media (max-width: 768px) {
  .messages-header {
    padding: 15px;
  }
  
  .messages-content {
    padding: 0 15px;
  }
  
  .message-item {
    padding: 12px;
  }
  
  .avatar-emoji {
    font-size: 36px;
  }
  
  .sender-name {
    font-size: 15px;
  }
  
  .last-message {
    font-size: 13px;
  }
  
  .fab {
    bottom: 90px;
    right: 15px;
    width: 50px;
    height: 50px;
  }
}
</style>